﻿using GIMS.Def;
using GIMS.Def.Operators;
using GIMS.Def.Settings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GIMS.GUI.GraphItems;

namespace GIMS.GUI.Test
{
    /// <summary>
    ///This is a test class for MainWindowTest and is intended
    ///to contain all MainWindowTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MainWindowTest
    {
        /// <summary>
        /// Reset GlobalSettings
        /// </summary>
        [TestInitialize()]
        public void MyTestInitialize()
        {
            GlobalSettings_Accessor.instance = null;
        }

        #region Psition:First

        /// <summary>
        ///A test for IsDropAllowed with an allowed first-operator. Position:First
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedFirst_FirstOperatorOnLast()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "load";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between-operator. Position:First
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedFirst_FirstOperatorOnBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "load";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed first-operator. Position:First
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedFirst_BetweenOperatorOnLast()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:First
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedFirst_BetweenOperatorOnBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a not allowed last operator. Position:First
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedFirst_LastOperator()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "save";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with allowed drop
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTestAllowed()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "load";
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with not allowed drop
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTestNotAllowed()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "resize";
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with save as target. Save does not have a parent so far, therfor drop is allowed.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedFirstOnSave()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "resize";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with save as target. Save does have a parent, therfor drop is not allowed.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedFirstOnSave()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "resize";
            Position position = Position.First;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            target.viewModel.Graph.AddNewVertex(targetVertex);
            target.viewModel.Graph.AddNewSourceToVertex(targetVertex, new CustomVertex(OperatorFactory.GetOperator("append")));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for IsDropAllowed with an target-operator that dont expects multiple input. Target already has a parent therefor drop is not allowed.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedExpectsMultipleFalse()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            var loadVertex = new CustomVertex(OperatorFactory.GetOperator("Load"));
            var saveVertex = new CustomVertex(OperatorFactory.GetOperator("Save"));
            target.viewModel.Graph.AddNewVertex(loadVertex);
            target.viewModel.Graph.AddNewVertexToSource(loadVertex, saveVertex);

            string operatorName = "Load";
            Position position = Position.First;
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, saveVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an target-operator that does expects multiple input.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedExpectsMultipleTrue()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            var loadVertex = new CustomVertex(OperatorFactory.GetOperator("Load"));
            var saveVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            target.viewModel.Graph.AddNewVertex(loadVertex);
            target.viewModel.Graph.AddNewVertexToSource(loadVertex, saveVertex);

            string operatorName = "Load";
            Position position = Position.First;
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, saveVertex);
            Assert.AreEqual(expected, actual);
        }

        #endregion Psition:First

        #region Position:Last

        /// <summary>
        ///A test for IsDropAllowed with an allowed last-operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedLast_LastOperatorOnFirst()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "save";
            Position position = Position.Last;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("load"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed last-operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedLast_FirstOperatorOnBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "save";
            Position position = Position.Last;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed first-operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedLast_BetweenOperatorOnFirst()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Last;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("load"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedLast_BetweenOperatorOnBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Last;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a not allowed last operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedLast_FirstOperator()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "load";
            Position position = Position.Last;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a second save operator. Position:Last
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedLast_SecondSave()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();

            var load = new CustomVertex(OperatorFactory.GetOperator("Load"));
            var save = new CustomVertex(OperatorFactory.GetOperator("Save")); 

            target.viewModel.Graph.AddNewVertex(load);
            target.viewModel.Graph.AddNewVertexToSource(load, save);

            string operatorName = "Save";
            Position position = Position.Last;

            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, load);
            Assert.AreEqual(expected, actual);
        }

        #endregion Psition:Last

        #region Position:Between

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedBetween_BetweenOperatorBetweenFirstLast()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Between;
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("load"));
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedBetween_BetweenOperatorBetweenFirstBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Between;
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("load"));
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedBetween_BetweenOperatorBetweenBetweenLast()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Between;
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("save"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with an allowed between operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_AllowedBetween_BetweenOperatorBetweenBetweenBetween()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Between;
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("crop"));
            bool expected = true;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a not allowed first operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedBetween_FirstOperator()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "load";
            Position position = Position.Between;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a not allowed last operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedBetween_LastOperator()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "save";
            Position position = Position.Between;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            CustomVertex sourceVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, sourceVertex);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsDropAllowed with a missing first operator. Position:Between
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsDropAllowedTest_NotAllowedBetween_MissingFirstOperator()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            string operatorName = "crop";
            Position position = Position.Between;
            CustomVertex targetVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            bool expected = false;
            bool actual;
            actual = target.IsDropAllowed(operatorName, position, targetVertex, null);
            Assert.AreEqual(expected, actual);
        }

        #endregion Position:Between

        /// <summary>
        ///A test for IsNewEdgeAllowed. Target vertex has a parent and does not expect multiple input therefore it's not allowed.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsNewEdgeAllowedTest1()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            var loadVertex = new CustomVertex(OperatorFactory.GetOperator("Load"));
            var saveVertex = new CustomVertex(OperatorFactory.GetOperator("Save"));
            target.viewModel.Graph.AddNewVertex(loadVertex);
            target.viewModel.Graph.AddNewVertexToSource(loadVertex, saveVertex);
            target.currentVertex = loadVertex;

            bool expected = false; 
            bool actual;
            actual = target.IsNewEdgeAllowed(saveVertex);
            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for IsNewEdgeAllowed. Target vertex has a parent and does expect multiple input therefore it's allowed.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("GIMS.GUI.exe")]
        public void IsNewEdgeAllowedTest2()
        {
            MainWindow_Accessor target = new MainWindow_Accessor();
            var loadVertex = new CustomVertex(OperatorFactory.GetOperator("Load"));
            var saveVertex = new CustomVertex(OperatorFactory.GetOperator("Append"));
            target.viewModel.Graph.AddNewVertex(loadVertex);
            target.viewModel.Graph.AddNewVertexToSource(loadVertex, saveVertex);
            target.currentVertex = loadVertex;

            bool expected = true;
            bool actual;
            actual = target.IsNewEdgeAllowed(saveVertex);
            Assert.AreEqual(expected, actual);
        }
    }
}
